package gms.dao.match;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import gms.entities.match.MatchApply;
import gms.entities.match.MatchInfo;

/**
 * 赛事申请表Dao实现类
 * @author damage
 *
 */
public class MatchApplyDaoImp extends HibernateDaoSupport implements MatchApplyDao{

	/**
	 * 保存赛事申请表
	 */
	@Override
	public boolean save(MatchApply matchApply) {
		this.getHibernateTemplate().save(matchApply);
		return true;
	}

	/**
	 * 查询赛事申请表总记录数
	 */
	@Override
	public int findCount() {
		String hql = "select count(*) from MatchApply";
		List<Long> list = (List<Long>) this.getHibernateTemplate().find(hql);
		if(list.size() > 0){
			return list.get(0).intValue();
		}
		return 0;
	}
	
	/**
	 * 分页查询赛事申请表
	 */
	@Override
	public List<MatchApply> findByPage(int begin, int pageSize) {
		DetachedCriteria criteria = DetachedCriteria.forClass(MatchApply.class);
		List<MatchApply> list = (List<MatchApply>) this.getHibernateTemplate().findByCriteria(criteria, begin, pageSize);
		for (MatchApply m : list) {
			System.out.println(m.toString());
		}
		return list;
	}

	/**
	 * 通过状态查找赛事申请表
	 */
	@Override
	public List<MatchApply> findByState(String state) {
		DetachedCriteria criteria = DetachedCriteria.forClass(MatchApply.class);
		//根据赛事的起始时间排序
		criteria.addOrder(Property.forName("startTime").asc());
		//添加查询条件
		criteria.add(Restrictions.eq("auditStatus",state));
		List<MatchApply> list = (List<MatchApply>) this.getHibernateTemplate().findByCriteria(criteria);
		for (MatchApply m : list) {
			System.out.println(m.toString());
		}
		return list;
	}

	/**
	 * 通过ID查询赛事申请表
	 */
	@Override
	public MatchApply findByID(int matchApplyID) {
		return this.getHibernateTemplate().get(MatchApply.class, matchApplyID);
	}

	/**
	 * 更新赛事申请表
	 */
	@Override
	public void update(MatchApply matchApply) {
		this.getHibernateTemplate().update(matchApply);
	}

	@Override
	public void toMatchInfo(MatchInfo matchInfo) {
		this.getHibernateTemplate().save(matchInfo);
	}

	@Override
	public void delete(MatchApply matchApply) {
		this.getHibernateTemplate().delete(matchApply);
		
	}


	
}
